package com.gimi.cloud.jimi.commonService.common.impl;

import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.commonService.common.JmOperationLogService;
import com.gimi.cloud.jimi.dao.dao.sys.SysOperationLogDao;
import com.gimi.cloud.jimi.dto.base.BaseDTO;
import com.gimi.cloud.jimi.dto.sys.SysOperationLogDTO;
import com.gimi.cloud.jimi.entity.sys.SysOperationLogEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * Created by bigsofty on 2018/1/2.
 */
@Service
public class JmOperationLogServiceImpl implements JmOperationLogService {

    private final SysOperationLogDao sysOperationLogDao;
    private static Logger logger = LoggerFactory.getLogger(JmOperationLogServiceImpl.class);

    @Autowired
    public JmOperationLogServiceImpl(SysOperationLogDao sysOperationLogDao){
        this.sysOperationLogDao = sysOperationLogDao;
    }


    @Override
    public void insertAddLog(BaseDTO dto,String tableId,String serviceName,String tableName,String desc) {
        try{
            SysOperationLogDTO logDTO = new SysOperationLogDTO();
            logDTO.setServiceName(serviceName);
            logDTO.setOperation("add");
            logDTO.setDesc(desc);
            logDTO.setTableName(tableName);
            logDTO.setTableId(tableId);
            logDTO.setOldData(null);
            logDTO.setNewData(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(dto));
            logDTO.setOperator(dto.getAddUid());
            logDTO.setCreateTime(new Date());
            insertMasterLog(logDTO);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void insertDeleteLog(Integer operatorId, String serviceName, String tableName,String tableId, String desc) {
        try{
            SysOperationLogDTO logDTO = new SysOperationLogDTO();
            logDTO.setServiceName(serviceName);
            logDTO.setOperation("delete");
            logDTO.setDesc(desc);
            logDTO.setTableName(tableName);
            logDTO.setTableId(tableId);
            logDTO.setOldData(null);
            logDTO.setNewData(null);
            logDTO.setOperator(operatorId);
            logDTO.setCreateTime(new Date());
            insertMasterLog(logDTO);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void insertQueryLog(BaseDTO dto, String serviceName, String tableName, String tableId, String desc) {
        try{
            SysOperationLogDTO logDTO = new SysOperationLogDTO();
            logDTO.setServiceName(serviceName);
            logDTO.setOperation("search");
            logDTO.setDesc(desc);
            logDTO.setTableName(tableName);
            logDTO.setTableId(tableId);
            logDTO.setOldData(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(dto));
            logDTO.setNewData(null);
            logDTO.setOperator(dto.getAddUid());
            logDTO.setCreateTime(new Date());
            insertMasterLog(logDTO);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void insertUpdateLog(BaseDTO dto, String serviceName, String tableName, String tableId, String desc) {
        try{
            SysOperationLogDTO logDTO = new SysOperationLogDTO();
            logDTO.setServiceName(serviceName);
            logDTO.setOperation("update");
            logDTO.setDesc(desc);
            logDTO.setTableName(tableName);
            logDTO.setTableId(tableId);
            logDTO.setOldData(null);
            logDTO.setNewData(JsonUtil.toJSONStringWithDateFormatYYYYMMDDHHMMSS(dto));
            logDTO.setOperator(dto.getAddUid());
            logDTO.setCreateTime(new Date());
            insertMasterLog(logDTO);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /**
     * 添加操作日志，操作日志不影响数据流程
     * @param dto
     */
    private void insertMasterLog(SysOperationLogDTO dto){
        try{
            SysOperationLogEntity sysOperationLogEntity = new SysOperationLogEntity();
            sysOperationLogEntity.setServiceName(dto.getServiceName());
            sysOperationLogEntity.setOperation(dto.getOperation());
            sysOperationLogEntity.setOperationDesc(dto.getDesc());
            sysOperationLogEntity.setTableName(dto.getTableName());
            sysOperationLogEntity.setTableId(dto.getTableId());
            sysOperationLogEntity.setOldData(dto.getOldData());
            sysOperationLogEntity.setNewData(dto.getNewData());
            sysOperationLogEntity.setOperator(dto.getOperator());
            sysOperationLogEntity.setCreateTime(dto.getCreateTime());
            sysOperationLogDao.insert(sysOperationLogEntity);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
